// 商品模型
const mongoose = require('./db')
const Schema = mongoose.Schema

let Product = new mongoose.Schema({
  name: { type: String, required: true, display: '商品名称' },
  item: { type: Schema.Types.ObjectId, required: true, ref: 'productItem', display: '商品分类' },
  baseCount: { type: Number, required: true, default: 100, display: '基础数量/库存' },
  salesCount: { type: Number, required: true, default: 0, display: '销量' },
  basePrice: { type: Number, required: true, default: 0.0, display: '成本价' },
  costPrice: { type: Number, required: true, default: 0.0, display: '原价' },
  price: { type: Number, required: true, default: 0.0, display: '销售价格' }, // ￥:分, 基础价格,销售价
  coupon: { type: [Schema.Types.ObjectId], required: false, display: '商品折扣', ref: 'coupon' },
  package: { type: [{
    name: { type: String, required: true, display: '套餐名称' },
    desc: { type: String, required: false, display: '套餐描述' },
    price: { type: Number, required: true, default: 0.0, display: '套餐价格' },
    previewImage: { type: String, required: false, display: '套餐图片' }
  }],required: false, display: '商品套餐' }, // 有套餐价格就以套餐为准
  desc: { type: String, required: true, display: '商品简介' },
  previewImage: { type: [String], required: true, display: '商品大图/封面图' }, // 列表显示第一张，详情页轮播
  content: { type: String, required: true,display: '商品详细介绍' },
  comment: { type: Schema.Types.ObjectId, required: true, display: '评论' }, // 先创建评论再创建商品
  rate: {
    count: { type: Number, required: false, display: '总评分' }, // 默认总评分，可以横向扩展其他评分
    times: { type: Number, required:false, display: '总评分人数' }
  },
  state: { type: Number, required: true, default: 200, display: '商品状态:[200:上架,201:下架,404:删除,302:预售]' },
  tags: { type: [{ name: { type: Schema.Types.ObjectId, required:false, ref: 'productTags' } }], required: false, display: '商品标签' },
}, {
  collection: 'product',
  timestamps: {
    createAt: true,
    updateAt: true,
  }
})

let product = mongoose.model('product', Product)


// 商品分类 <---> 商品模型.item
let ProductItem = new mongoose.Schema({
  name: { type: String, required: true, default: '未分类', display: '分类名称' },
  desc: { type: String, required: true, default: '', display: '分类简介' },
  deep: { type: Number, required: true, default: 1, display: '分类级别' },
  child: [{ type: Schema.Types.ObjectId, required: false, ref: 'productItem', display: '子级分类' }],
  previewImage: { type: String, required: false, display: '分类图片' }
}, {
  collection: 'product_item',
  timestamps: {
    createAt: true,
    updateAt: true,
  }
})

let productItem = mongoose.model('productItem', ProductItem)


// 商品标签 <---> 商品模型.tags
let ProductTags = new mongoose.Schema({
  name: { type: String, required: true, display: '标签名字' },
  desc: { type: String, required: false, display: '标签描述' },
}, {
  collection: 'product_tas',
  timestamps: {
    createAt: true,
  }
})

let productTags = mongoose.model('productTags', ProductTags)


// 商品评论
let Comment = new mongoose.Schema({
  username: { type: String, required: true, default: '匿名', display: '评论者昵称' },
  uid: { type: Schema.Types.ObjectId, required: true, ref: 'user', display: '评论者ID' },
  count: { type: Number, default: 5.0, required: true, display: '评分分数' },
  tags: { type: String, required: false, display: '评分tag,逗号分隔' },
  desc: { type: String, required: false, display: '评论内容' },
}, {
  collection: 'product_comment',
  timestamps: {
    createAt: true,
    updateAt: true,
  }
})

let productComment = mongoose.model('productComment', Comment)

module.exports = {
  product: product,
  productItem: productItem,
  productTags: productTags,
  productComment: productComment,
}